// pages/browsingHistory/browsingHistory.js
const app = getApp()
var service = require('../../util/service')
var util = require('../../util/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = app.userInfo;
    service.AJAX(`smc/topic/LookHistory/listLookHistory.json?$relType=Provider&$userId=${user.buyId}`, {}, res => {
      console.log(res);
      var ids = [];
      for (var i = 0; i < res.length;i++){
        ids.push(res[i].relId);
      }
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          that.setData({
            lat: res.latitude,
            lon: res.longitude,
          });
      //获取机构
      service.AJAX("smc/base/SystemProviderUser/listSystemProviderUser.json?$prvId-in=" + ids.toString(), {}, res => {
        var orgList = res;
        for (var i = 0; i < orgList.length; i++) {
          var price = orgList[i].minPrice + "";
          var activitys = orgList[i].prodLabel;
          if (activitys && activitys != "") {
            orgList[i].activity = activitys.split(/[；;]/);
          }
          var prvDesc = orgList[i].prvDesc;
          if (prvDesc && prvDesc.length > 30) {
            orgList[i].prvDesc = prvDesc.substr(0, 30) + "……";
          }
          orgList[i].attribute1 = service.imgurl + orgList[i].attribute1;
          orgList[i].minPrice = price.indexOf(".") > 0 ? price.split(".") : (price + ".00").split(".");
          orgList[i].distance = util.getDistance(that.data.lat, that.data.lon, orgList[i].posX, orgList[i].posY);
        }
        that.setData({
          orgList: orgList,
        })
      }, err => { }, 'GET')
        }
      });
    }, err => { }, 'GET', true)
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})